package com.finger.crowd.service.api.impl;

import com.finger.crowd.entity.po.MemberConfirmInfoPO;
import com.finger.crowd.entity.po.MemberLaunchInfoPO;
import com.finger.crowd.entity.po.ProjectPO;
import com.finger.crowd.entity.po.ReturnPO;
import com.finger.crowd.entity.vo.*;
import com.finger.crowd.mapper.MemberConfirmInfoPOMapper;
import com.finger.crowd.mapper.MemberLaunchInfoPOMapper;
import com.finger.crowd.mapper.ProjectPOMapper;
import com.finger.crowd.mapper.ReturnPOMapper;
import com.finger.crowd.service.api.ProjectService;
import com.finger.crowd.util.ResultEntity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.transform.Result;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {

    private Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Autowired
    private ProjectPOMapper projectPOMapper;

    @Autowired
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Autowired
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Autowired
    private ReturnPOMapper returnPOMapper;

    // propagation设置当有一个事务插入到此事务时要开辟一个新的事务
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public ResultEntity saveProjectVO(ProjectVO projectVO, Integer memberId) {

        logger.info(projectVO.toString());

        // 保存projectPO对象
        ProjectPO projectPO = new ProjectPO();

        BeanUtils.copyProperties(projectVO, projectPO);

        Date date = new Date();

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:");

        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");

        String createTime = simpleDateFormat1.format(date);

        String deployedDate = simpleDateFormat2.format(date);

        projectPO.setDeploydate(deployedDate);

        projectPO.setCreatedate(createTime);

        projectPO.setMemberid(memberId);

        projectPO.setStatus(0);

        projectPOMapper.insertSelective(projectPO);

        Integer projectId = projectPO.getId();

        // 保存分类id
        List<Integer> typeIdList = projectVO.getTypeIdList();

        projectPOMapper.insertTypeRelationship(typeIdList, projectId);

        // 保存标签id
        List<Integer> tagIdList = projectVO.getTagIdList();

        projectPOMapper.insertTagRelationship(tagIdList, projectId);

        // 保存项目详情图片路径
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();

        projectPOMapper.insertDetailPicturePaths(detailPicturePathList, projectId);

        // 保存发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();

        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();

        memberLaunchInfoPO.setMemberid(memberId);

        BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);

        memberLaunchInfoPOMapper.insertSelective(memberLaunchInfoPO);

        // 保存回报信息
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();

        List<ReturnPO> returnPOList = new ArrayList<ReturnPO>();

        for (ReturnVO returnVO : returnVOList) {

            ReturnPO returnPO = new ReturnPO();

            BeanUtils.copyProperties(returnVO, returnPO);

            returnPOList.add(returnPO);
        }
        returnPOMapper.insertReturnPOList(returnPOList, projectId);

        // 保存发起人确认信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();

        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();

        BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);

        memberConfirmInfoPO.setMemberid(memberId);

        memberConfirmInfoPOMapper.insertSelective(memberConfirmInfoPO);

        return ResultEntity.successWithOutData();
    }

    public List<PortalTypeVO> getAllPortalProject() {

        List<PortalTypeVO> portalTypeVOS = projectPOMapper.selectPortalTypeVOList();

        return portalTypeVOS;
    }

    public DetailProjectVO getDetailProjectVO(Integer projectId) {
        // 1.查询得到 DetailProjectVO 对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        // 2.根据 status 确定 statusText
        Integer status = detailProjectVO.getStatus();
        switch (status) {
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;
            default:
                break;
        }
        // 3.根据 deployeDate 计算 lastDay
        // 2020-10-15
        String deployDate = detailProjectVO.getDeployDate();
        // 获取当前日期
        Date currentDay = new Date();
        // 把众筹日期解析成 Date 类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date deployDay = format.parse(deployDate);
            // 获取当前当前日期的时间戳
            long currentTimeStamp = currentDay.getTime();
            // 获取众筹日期的时间戳
            long deployTimeStamp = deployDay.getTime();
            // 两个时间戳相减计算当前已经过去的时间
            long pastDays = (currentTimeStamp - deployTimeStamp) / 1000 / 60 / 60 / 24;
            // 获取总的众筹天数
            Integer totalDays = detailProjectVO.getDay();
            // 使用总的众筹天数减去已经过去的天数得到剩余天数
            Integer lastDay = (int) (totalDays - pastDays);
            detailProjectVO.setLastDay(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return detailProjectVO;
    }
}
